import request from '@/utils/request'

const BookService = {
  // 根据ISBN获取图书
  getBookByISBN(isbn) {
    console.log('Fetching book with ISBN:', isbn);
    return request({
      url: `/books/${isbn}`,
      method: 'get'
    }).catch(error => {
      console.error('Error fetching book:', error.response?.data || error);
      throw error;
    });
  },

  // 添加图书
  addBook(book) {
    console.log('Adding book:', book);
    // 转换字段名以匹配数据库
    const dbBook = {
      ...book,
      purchase_discount: book.purchaseDiscount,
      sales_discount: book.salesDiscount,
      publish_date: book.publishDate ? new Date(book.publishDate).toISOString().split('T')[0] : null,
      date: book.date ? new Date(book.date).toISOString().split('T')[0] : null,
      book_type: book.type  // 添加图书类型字段
    };

    // 删除不需要的字段
    delete dbBook.purchaseDiscount;
    delete dbBook.salesDiscount;
    delete dbBook.publishDate;
    delete dbBook.type;  // 删除前端的 type 字段

    // 处理标题拼接
    if (dbBook.book_type === '教辅' && book.subject && book.grade) {
      dbBook.title = `${book.title}${book.grade}${book.subject}`;
    }

    console.log('Converted book data:', dbBook);

    return request({
      url: '/books',
      method: 'post',
      data: dbBook
    }).catch(error => {
      console.error('Error adding book:', error.response?.data || error);
      throw error;
    });
  },

  // 更新图书
  updateBook(isbn, book) {
    console.log('Updating book:', isbn, book);
    // 转换字段名以匹配数据库
    const dbBook = {
      ...book,
      purchase_discount: book.purchaseDiscount,
      sales_discount: book.salesDiscount,
      publish_date: book.publishDate ? new Date(book.publishDate).toISOString().split('T')[0] : null,
      date: book.date ? new Date(book.date).toISOString().split('T')[0] : null,
      book_type: book.type  // 添加图书类型字段
    };

    // 删除不需要的字段
    delete dbBook.type;  // 删除 type 字段，因为已经转换为 book_type
    delete dbBook.purchaseDiscount;
    delete dbBook.salesDiscount;
    delete dbBook.publishDate;

    // 处理标题拼接
    if (dbBook.book_type === '教辅' && book.subject && book.grade) {
      dbBook.title = `${book.title}${book.grade}${book.subject}`;
    }

    console.log('Converted book data:', dbBook);

    return request({
      url: `/books/${isbn}`,
      method: 'put',
      data: dbBook
    }).catch(error => {
      console.error('Error updating book:', error.response?.data || error);
      throw error;
    });
  },

  // 删除图书
  deleteBook(isbn) {
    console.log('Deleting book:', isbn);
    return request({
      url: `/books/${isbn}`,
      method: 'delete'
    }).catch(error => {
      console.error('Error deleting book:', error.response?.data || error);
      throw error;
    });
  },

  // 获取所有图书
  getAllBooks() {
    console.log('Fetching all books');
    return request({
      url: '/books',
      method: 'get'
    }).catch(error => {
      console.error('Error fetching books:', error.response?.data || error);
      throw error;
    });
  }
}

export default BookService
